package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    private DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
    private User user = new User();
    private UserDaoImpl userDao = new UserDaoImpl();

    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
        List<DeliveryAddress> list = new ArrayList<>();
        try {
            DBUtil.getConnection();
            list = dao.listDeliveryAddressByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public DeliveryAddress getDeliveryAddressById(int daId) {
        DeliveryAddress address = new DeliveryAddress();
        try {
            DBUtil.getConnection();
            address = dao.getDeliveryAddressById(daId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return address;
    }

    @Override
    public int saveDeliveryAddress(DeliveryAddress address) {
        int row = 0;
        try {
            DBUtil.getConnection();
            row = dao.saveDeliveryAddress(address);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return row;
    }

    @Override
    public int updateDeliveryAddress(DeliveryAddress address) {
        int row = 0;
        try {
            DBUtil.getConnection();
            row = dao.updateDeliveryAddress(address);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return row;
    }

    @Override
    public int removeDeliveryAddress(int daId) {
        int row = 0;
        try {
            DBUtil.getConnection();
            row = dao.removeDeliveryAddress(daId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return row;
    }
}
